/*   jQuery Lightbox Modal Copyright (C) 2011  Adriano Lurati

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
* 	@Author Adriano Lurati
* 	@version 1.0
* Options [
*   @param center: Default( TRUE ) could be true or false. This center the element in the browser.
*   @param closeClass: Default ( '.closeModal' ) This is the class of the close button. if uses the default in all the modals, any close button with this class will close all the other windows with the same close class. Or you can just set a new class for every close button in the modals.
*   @param event: Default ('click') This is the event of the element that who is going to open the modal.
*   @param left: Default ( 0 ) This just set a left attr to the modal css. if center is TRUE this will move the modal N pixels to left / right ( negative number )
*   @param position: Default ('fixed') This just set a position attr to the modal css.
*   @param shadowbox: Default ( FALSE ) This able or disable the shadowbox around the modal.
*   @param selector: Default ( NOTHING ) This is the selector of the modal that one is going to be opened.
*   @param top: Default ( 0 ) This just set a top attr to the modal css. if center is TRUE this will move the modal N pixels to bottom / top ( negative number )
* ]

*/
(function ($) {
	$.topZIndex = function (selector) {
		return Math.max(0, Math.max.apply(null, $.map($(selector || "body *"), 
			function (v) {
				return parseInt($(v).css("z-index")) || null;
			}
		)));    
	};
    $.fn.centerElement = function(options) {
        var defaults = {
            center: true,
            closeClass: '.closeModal',
            event: 'click',
            left: 0,
            position: 'fixed',
            shadowbox: false,
            selector: $(this),
            top: 0
        };
        var options = $.extend(defaults, options);
        return this.each(function() {
            $(this).bind(options.event, init);
        });
        function init(e) {
            e.preventDefault();
            $(options.selector).hide().fadeIn('slow');
            var centerHorizontal = (($(window).width() - $(options.selector).outerWidth()) / 2),
                centerVertical = (($(window).height() - $(options.selector).outerHeight()) / 2);
            if (options.shadowbox) {
                $('body').append('<div id="shadowbox"></div>')
                $('#shadowbox').css({
                    'background': '#000',
                    'height': $(document).height() + 'px',
                    'opacity': 0.5,
                    'position': 'fixed',
                    'top': 0,
                    'left': 0,
                    'width': $(document).width() + 'px',
                    'z-index': $.topZIndex()
                });
                
            }
            $(options.selector).css({
                'position': options.position,
                'z-index': $.topZIndex() + 1
            });
            if (options.center) {
                $(options.selector).css({
                    'left': centerHorizontal + (options.left) + 'px',
                    'top': centerVertical + (options.top) + 'px'
                });
            } else {
                $(options.selector).css({
                    'left': (options.left) + 'px',
                    'top': (options.top) + 'px'
                });
            }
            $(options.closeClass).click(function(e) {
                e.preventDefault();
                $(options.selector).fadeOut('slow');
                $('#shadowbox').remove();
            });
        };
    }
})(jQuery);
